package blog.controller;

import blog.entity.Comment;
import blog.service.CommentService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller @RequestMapping("/comment")
public class CommentController {
    @Resource
    private CommentService commentService;

    @RequestMapping("/delete/{id}") @ResponseBody
    public String deleteComment(@PathVariable("id") Integer id){
        commentService.deleteCommentById(id);
        return "删除成功";
    }

    @RequestMapping("")
    public String gotoComment(@RequestParam(required = false,defaultValue = "1") Integer pageIndex, @RequestParam(required = false,defaultValue = "8") Integer pageSize,Map map){
        PageInfo<Comment> pageInfo = commentService.getPageArticleList(pageIndex,pageSize);
        map.put("pageInfo",pageInfo);
        map.put("pageUrlPrefix","comment?pageIndex");
        return "comment/comment-list";
    }
    @RequestMapping("/reply/{id}")
    public String gotoReply(@PathVariable("id") Integer id, ModelMap map){
        Comment comment = commentService.getCommentById(id);
        String pCommentContent = commentService.getCommentById(comment.getCommentPid()).getCommentContent();
        map.put("pCommentContent",pCommentContent);
        map.addAttribute("comment",comment);
        return "comment/comment-reply";
    }
    @RequestMapping("/replySubmit")
    public String reply(Comment comment){
       comment.setCommentIp("0:0:0:0:0:0:0:1");
       comment.setCommentCreateTime(new Date());
       commentService.addComment(comment);
      return "forward:/comment";
    }
    @RequestMapping("/edit/{id}")
    public String gotoEdit(@PathVariable("id") Integer id,ModelMap map){
        Comment comment = commentService.getCommentById(id);
        String pCommentContent = commentService.getCommentById(comment.getCommentPid()).getCommentContent();
        map.put("pCommentContent",pCommentContent);
        map.addAttribute("comment",comment);
        return "comment/comment-edit";
    }
    @RequestMapping("/editComment")
    public String editComment(Comment comment){
        commentService.updateComment(comment);
        return "forward:/comment";
    }
}
